﻿using System;
using System.Collections;
using System.Data;
using System.Web.UI;
using AMS.Core.Entities;
using AMS.History.Entities;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;
using VitCode.Web.UI.WebControls;

namespace AMS.Web.BaseData
{
    public partial class AssetDepreciationEdit : Page
    {

        BaseDataManager m_Acc = new BaseDataManager();
        private static  AssetInfoRecord m_AssetInfoRecord = new AssetInfoRecord();
        private static HisVersionInfo m_HisVersionInfo = new HisVersionInfo();
        private static int iHervisionId;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                ZLTextBox_DepreciationValue.Focus();
                if (Request.QueryString["Id"] != null)
                {
                    string strAssetInfoId = Request.QueryString["Id"].Trim();
                    if (strAssetInfoId.Length > 0 && strAssetInfoId.Length == 36)
                    {
                        LabelId.Text = strAssetInfoId.Trim();
                        IList objList = m_Acc.GetObjectList(new AssetInfo().GetType().Name, "Id", strAssetInfoId.Trim());
                        if (objList != null && objList.Count > 0)
                        {
                            var currentAssetInfo = (AssetInfo)objList[0];
                            ZLTextBox_Barcode.Text = currentAssetInfo.Barcode;
                            ZLTextBox_EndValue.Text = currentAssetInfo.EndValue.ToString();
                            ZLTextBox_AssetName.Text = currentAssetInfo.Name;
                            if (Request.Cookies["LoginUserId"] != null)
                                m_HisVersionInfo.OperatorUserId = new Guid(Request.Cookies["LoginUserId"].Value);
                            m_HisVersionInfo.CreateDate = DateTime.Now;
                            m_HisVersionInfo.Memo = "资产折旧";

                            DataTable dt = m_Acc.GetDataTableBySql("select max(Id) from AmsHistory_HisVersionInfo");
                            if (dt != null && dt.Rows.Count > 0)
                            {
                                iHervisionId = dt.Rows[0][0].ToString().Length > 0 ? Int32.Parse(dt.Rows[0][0].ToString()) : 1;
                                SaveAssetInfoRecord(currentAssetInfo);
                            }
                        }
                    }

                }
            }
         }

        private void SaveAssetInfoRecord(AssetInfo currentAssetInfo)
        {
            m_AssetInfoRecord.HVersionId = iHervisionId + 1;
            m_AssetInfoRecord.AssetTypeId = currentAssetInfo.AssetType.Id;
            m_AssetInfoRecord.Barcode = currentAssetInfo.Barcode;
            m_AssetInfoRecord.CheckedDate = currentAssetInfo.CheckedDate;
            m_AssetInfoRecord.Code = currentAssetInfo.Code;
            m_AssetInfoRecord.DepartmentId = (currentAssetInfo.Department == null ? Guid.Empty : currentAssetInfo.Department.Id);
            m_AssetInfoRecord.DepreciationFunction = currentAssetInfo.DepreciationFunction;
            m_AssetInfoRecord.DepreciationRate = currentAssetInfo.DepreciationRate;
            m_AssetInfoRecord.DepreciationValue = currentAssetInfo.DepreciationValue;
            m_AssetInfoRecord.DepreciationYears = currentAssetInfo.DepreciationYears;
            m_AssetInfoRecord.HBaseObjectId = currentAssetInfo.Id;
            m_AssetInfoRecord.HId = new Guid();
            m_AssetInfoRecord.Location = currentAssetInfo.Location;
            m_AssetInfoRecord.Manufacturers = currentAssetInfo.Manufacturers;
            m_AssetInfoRecord.Memo = currentAssetInfo.Memo;
            m_AssetInfoRecord.ModelNumber = currentAssetInfo.ModelNumber;
            m_AssetInfoRecord.Name = currentAssetInfo.Name;
            m_AssetInfoRecord.NatureId = (currentAssetInfo.Nature == null ? Guid.Empty : currentAssetInfo.Nature.Id);
            m_AssetInfoRecord.OriginalValue = currentAssetInfo.OriginalValue;
            m_AssetInfoRecord.ParentBarcode = currentAssetInfo.ParentBarcode;
            m_AssetInfoRecord.SerialNumber = currentAssetInfo.SerialNumber;
            m_AssetInfoRecord.ShelfLife = currentAssetInfo.ShelfLife;
            m_AssetInfoRecord.SiteId = (currentAssetInfo.Site == null ? Guid.Empty : currentAssetInfo.Site.Id);
            m_AssetInfoRecord.SizeInfo = currentAssetInfo.SizeInfo;
            m_AssetInfoRecord.SourceId = (currentAssetInfo.Source == null ? Guid.Empty : currentAssetInfo.Source.Id);
            m_AssetInfoRecord.StartUpDate = currentAssetInfo.StartUpDate;
            m_AssetInfoRecord.StateId = (currentAssetInfo.State == null ? Guid.Empty : currentAssetInfo.State.Id);
            m_AssetInfoRecord.Supplier = currentAssetInfo.Supplier;
            m_AssetInfoRecord.TypeName = currentAssetInfo.TypeName;
            m_AssetInfoRecord.UnitId = (currentAssetInfo.Unit == null ? Guid.Empty : currentAssetInfo.Unit.Id);
            m_AssetInfoRecord.UsersId = (currentAssetInfo.Users == null ? Guid.Empty : currentAssetInfo.Users.Id);
            m_AssetInfoRecord.UseYears = currentAssetInfo.UseYears;
           
           
           

        }

      

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Window.Close();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
              if(LabelId.Text.Length == 36)
              {
                  if(Convert.ToDouble(ZLTextBox_DepreciationValue.Text.Trim()) > Convert.ToDouble(ZLTextBox_EndValue.Text.Trim()))
                  {
                      Alert.MessageBox("折旧值不能大于资产净值，不能折旧!");
                      return;
                  }
                  IList objList = m_Acc.GetObjectList(new AssetInfo().GetType().Name, "Id", LabelId.Text.Trim());
                  if(objList != null && objList.Count > 0)
                  {
                      var currentAssetInfo = (AssetInfo)objList[0];
                      currentAssetInfo.DepreciationValue += Convert.ToDouble(ZLTextBox_DepreciationValue.Text.Trim());
                      currentAssetInfo.EndValue -= Convert.ToDouble(ZLTextBox_DepreciationValue.Text.Trim());
                      var access =  m_Acc.SaveOrUpdate(currentAssetInfo);
                      if(access)
                      {
                         
                          m_HisVersionInfo.Memo = "资产手动折旧，折旧值:[" + ZLTextBox_DepreciationValue.Text + "]元";
                          m_Acc.Session.Save(m_HisVersionInfo);
                          m_AssetInfoRecord.HId = new Guid();
                          m_AssetInfoRecord.DepreciationValue += Convert.ToDouble(ZLTextBox_DepreciationValue.Text.Trim());
                          m_AssetInfoRecord.HisMemo = "AssetDepreciation";
                          m_Acc.SaveOrUpdate(m_AssetInfoRecord);
                          Alert.MessageBox("折旧成功!");
                          btnSubmit.Enabled = false;
                          Session.Add("Close", "Yes");
                      }
                     
                  }
              }
        }
    }
}
